package com.fand.manager.config;

import javax.annotation.Resource;

import com.fand.stater.handler.SysUserESIndexRecordHandler;
import com.fand.manager.handler.SysAuthenticationFailureHandler;
import com.fand.manager.handler.SysAuthenticationSuccessHandler;
import com.fand.manager.service.impl.SysUserDetailService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * Author: zhangjiabin
 * Email: javaboyok@163.com
 * CreateDate: 2021/5/30
 * Version: 1.0
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private SysUserDetailService        userDetailService;
    @Resource
    private SysUserESIndexRecordHandler sysUserESIndexRecordHandler;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                // 如果有允许匿名的url，填在下面
                .anyRequest().authenticated()
                .and()
                // 设置登陆页
                .formLogin().loginPage("/login_p").loginProcessingUrl("/login")
                .successHandler(new SysAuthenticationSuccessHandler(sysUserESIndexRecordHandler))
                .failureHandler(new SysAuthenticationFailureHandler())
                .permitAll()
                // 自定义登陆用户名和密码参数，默认为username和password
                .usernameParameter("username")
                .passwordParameter("password")
                .and()
                .rememberMe().tokenValiditySeconds(60*60*6)
                .and()
                .logout().permitAll()
                .and()
                .headers().frameOptions().disable();

        // 关闭CSRF跨域
        http.csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/swagger/**")
                .antMatchers("/swagger-ui.html")
                .antMatchers("/webjars/**")
                .antMatchers("/v2/**")
                .antMatchers("/v2/api-docs-ext/**")
                .antMatchers("/swagger-resources/**")
                .antMatchers("/doc.html")
                .antMatchers("/images/**")
                .antMatchers("/captcha/**")
                .antMatchers("/verify")
                .antMatchers("/static/**");
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

}
